<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片主体区域 -->
    <el-card>
      <!-- 添加角色按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 角色列表 -->
      <el-table :data="rolesList" border style="width: 100%" stripe>
        <!-- 展开行 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              v-for="(item1, index1) in scope.row.children"
              :key="item1.id"
              :class="['borderBottom',index1 === 0?'borderTop' : '','vCenter']"
            >
              <!-- 一级权限 -->
              <el-col :span="5">
                <el-tag closable @close="deleteRights(scope.row,item1.id)">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级三级权限 -->
              <el-col :span="18">
                <el-row
                  v-for="(item2,index2) in item1.children"
                  :key="item2.id"
                  :class="[index2 === 0? '': 'borderTop','vCenter']"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="deleteRights(scope.row,item2.id)"
                    >{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      type="warning"
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      closable
                      @close="deleteRights(scope.row,item3.id)"
                    >{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="showSetRightDialog(scope.row)"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限对话框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="setRightDialogVisible"
      width="50%"
      @close="rightDialogClose"
    >
      <el-tree
        :data="rightsList"
        :props="rightsProps"
        node-key="id"
        :default-checked-keys="defKeys"
        show-checkbox
        default-expand-all
        ref="setRightDialogRef"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmRightDialog">确 定</el-button>
        <el-button @click="setRightDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as allRightsFunc from '@/network/rights'
export default {
  name: 'RolesList',
  data() {
    return {
      rolesList: [],
      setRightDialogVisible: false,
      rightsList: [],
      rightsProps: {
        children: 'children',
        label: 'authName'
      },
      defKeys: [],
      currentRoleMessage: {}
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    async getRolesList() {
      const { data, meta } = await allRightsFunc.getRolesData('list')
      if (meta.status !== 200) {
        return this.$message.error(meta.msg)
      } else {
        this.rolesList = data
      }
    },
    // 删除展开列中的权限
    deleteRights(role, rids) {
      this.$confirm('此操作将永久删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          console.log('确认删除')
          const { data, meta } = await allRightsFunc.deleteRightsById(
            role.id,
            rids
          )
          if (meta.status !== 200) {
            return this.$message.error(meta.msg)
          }
          role.children = data
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 点击分配权限按钮
    async showSetRightDialog(role) {
      this.currentRoleMessage = role
      const { data, meta } = await allRightsFunc.getRightsData('tree')
      if (meta.status !== 200) {
        return this.$message.error(meta.msg)
      }
      this.rightsList = data
      this.setDefKeys(role)
      this.setRightDialogVisible = true
    },
    setDefKeys(node) {
      if (!node.children) {
        return this.defKeys.push(node.id)
      }
      node.children.forEach(item => {
        this.setDefKeys(item)
      })
    },
    rightDialogClose() {
      this.defKeys = []
    },
    async confirmRightDialog() {
      const selectRights = [
        ...this.$refs.setRightDialogRef.getCheckedKeys(),
        ...this.$refs.setRightDialogRef.getHalfCheckedKeys()
      ]
      const selectRightsStr = selectRights.join(',')
      const { meta } = await allRightsFunc.roleAuthorization(
        this.currentRoleMessage.id,
        selectRightsStr
      )
      if (meta.status !== 200) {
        return this.$message.error(meta.msg)
      }
      this.$message.success(meta.msg)
      this.getRolesList()
      this.setRightDialogVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.el-tag {
  margin: 7px;
}
.borderBottom {
  border-bottom: 1px solid #eeeeee;
}
.borderTop {
  border-top: 1px solid #eeeeee;
}

.vCenter {
  display: flex;
  align-items: center;
}
</style>
